/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class Account_TestLeadDeleteUndelete {

    static testMethod void myUnitTest() {
        // Create an Account
        Account TestAccount = new Account(Name='Test Account');
        insert TestAccount;
        
        // Create a Lead
        Lead TestLead = new Lead(LastName='Test',Company='Test Account',Account__c = TestAccount.Id);
        insert TestLead;
        
        // Delete the account
        delete TestAccount;
        
        // Verify that the Lead is deleted
        TestLead = [select Id,IsDeleted from Lead where Id = :TestLead.Id ALL ROWS];
        System.assertEquals(True,TestLead.IsDeleted);
        
        // Undelete the Account
        test.starttest();
        undelete TestAccount;
        test.stoptest();
        
        // Verify that the Lead is undeleted
        TestLead = [select Id,IsDeleted from Lead where Id = :TestLead.Id];
        System.assert(!TestLead.IsDeleted);
    }
    
    static testMethod void myBatchDeleteTest() {
    	// Create 200 Accounts
    	List<Account> TestAccounts = new List<Account>();
    	for(Integer i=0;i<200;i++){
    		TestAccounts.add(new Account(Name='Test Account ' + i));
    	}
    	insert TestAccounts;
    	
    	// Create two Leads at each
    	List<Lead> TestLeads = new List<Lead>();
    	for(Account a : TestAccounts){
    		TestLeads.add(new Lead(LastName='Test',Company='Test Account',Account__c = a.Id));
    		TestLeads.add(new Lead(LastName='Test',Company='Test Account',Account__c = a.Id));
    	}
    	
    	// Create a Set of Account IDs
    	Set<Id> AccountIds = new Set<Id>();
    	for(Account a : TestAccounts){
    		AccountIds.add(a.Id);
    	}
    	
    	// Start Test
    	test.starttest();
    	
    	// Delete the Accounts
    	delete TestAccounts;
    	
    	// Stop Test
    	test.stoptest();
    	
    	// Verify that the Leads are deleted
    	TestLeads = [select Id, IsDeleted from Lead where Account__c in :AccountIds ALL ROWS];
    	for(Lead l : TestLeads){
    		System.Assert(l.IsDeleted);
    	}
    	
    }
    
    static testMethod void myBatchUndeleteTest() {
    	// Create 100 Accounts
    	List<Account> TestAccounts = new List<Account>();
    	for(Integer i=0;i<100;i++){
    		TestAccounts.add(new Account(Name='Test Account ' + i));
    	}
    	insert TestAccounts;
    	
    	// Create two Leads at each
    	List<Lead> TestLeads = new List<Lead>();
    	for(Account a : TestAccounts){
    		TestLeads.add(new Lead(LastName='Test',Company='Test Account',Account__c = a.Id));
    		TestLeads.add(new Lead(LastName='Test',Company='Test Account',Account__c = a.Id));
    	}
    	
    	// Create a Set of Account IDs
    	Set<Id> AccountIds = new Set<Id>();
    	for(Account a : TestAccounts){
    		AccountIds.add(a.Id);
    	}
    	
    	// Delete the Accounts
    	delete TestAccounts;
    	
    	// Start Test
    	test.starttest();
    	
    	// Undelete the Account
    	undelete TestAccounts;
    	
    	// Stop Test
    	test.stoptest();
    	
    	// Verify that the Leads are undeleted
    	TestLeads = [select Id, IsDeleted from Lead where Account__c in :AccountIds ALL ROWS];
    	for(Lead l : TestLeads){
    		System.Assert(!l.IsDeleted);
    	}
    }
}